package com.soda.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class BaseDao {

	private static String CLASSNAME;	
	private static String URL;
	private static String USERNAME;
	private static String PASSWORD;
	
	/*static{
		
		ResourceBundle rb = ResourceBundle.getBundle("jdbc.properties");
		
		CLASSNAME=rb.getString("driver");
		URL=rb.getString("url");
		USERNAME=rb.getString("username");
		PASSWORD=rb.getString("password");
		
		
	}*/
	
	static {
		Properties prop = new Properties();
		InputStream is  = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
		try {
			prop.load(is);
			CLASSNAME = prop.getProperty("driver");
			URL = prop.getProperty("url");
			USERNAME = prop.getProperty("user");
			PASSWORD = prop.getProperty("password");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	protected Connection conn=null;
	protected PreparedStatement pt=null;
	protected ResultSet res=null;
	
	public Connection getConnection(){
		try {
			Class.forName(CLASSNAME);
			conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
			return conn;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public void close(){
		try {
			if(res!=null)
				res.close();
			if(pt!=null)
				pt.close();
			if(conn!=null)
				conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}

}

